package com.yunhe.policyfulfillment.authority.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.policyfulfillment.authority.domain.PolicyRole;
import com.yunhe.policyfulfillment.authority.model.PolicyRoleQueryCondition;
import com.yunhe.policyfulfillment.authority.service.PolicyRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhengbo
 * @date 2021/9/19 15:29
 */
@RestController
@RequestMapping(value = "/policyRole")
@Api(value = "policyRole", description = "角色", tags = {"Model_PolicyRole"})
public class PolicyRoleController {

    @Autowired
    private PolicyRoleService policyRoleService;

    @ApiOperation("新增角色")
    @PostMapping("/createPolicyRole")
    public ResponseEntity<ResultObject> createRole(@RequestBody PolicyRole policyRole) throws ArgumentErrorException {
        policyRoleService.createPolicyRoleSelective(policyRole);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    @ApiOperation("更新角色")
    @PostMapping("/updatePolicyRole")
    public ResponseEntity<ResultObject> updateRole(@RequestBody PolicyRole policyRole) throws ArgumentErrorException {
        policyRoleService.updatePolicyRoleByPrimaryKeySelective(policyRole);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    @ApiOperation("删除角色")
    @PostMapping("/deleteRole")
    public ResponseEntity<ResultObject> deleteRole(@RequestParam(value = "ids") String ids) throws ArgumentErrorException {
        policyRoleService.deletePolicyRoleByIds(ids);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    @ApiOperation("查询角色列表")
    @PostMapping("/searchRoleList")
    public ResponseEntity<ResultObject<List<PolicyRole>>> searchRoleList(@RequestBody PolicyRoleQueryCondition policyRoleQueryCondition) {
        List<PolicyRole> policyRoleList = policyRoleService.searchPolicyRoleList(policyRoleQueryCondition);
        return new ResponseEntity<>(new ResultObject<>(policyRoleList), HttpStatus.OK);
    }

    @ApiOperation("查询角色列表(分页)")
    @PostMapping("/searchRolePage")
    public ResponseEntity<ResultObject<PageResult<PolicyRole>>> searchRolePage(@RequestBody PolicyRoleQueryCondition policyRoleQueryCondition) {
        PageResult<PolicyRole> result = policyRoleService.searchPolicyRoleForPage(policyRoleQueryCondition);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("角色绑定/解绑菜单")
    @PostMapping("/roleBindOrUnbindMenu")
    public ResponseEntity<ResultObject> roleBindOrUnbindMenu(@RequestParam(value = "roleId") Long roleId,
                                                             @RequestParam(value = "menuIds") String menuIds) {
        policyRoleService.roleBindOrUnbindMenu(roleId, menuIds);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }
}
